package com.example.demo.user.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.topic.entity.TCollectRecord;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.example.demo.user.service.IUAccountCollectService;
import com.example.demo.user.entity.UAccountCollect;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 主体账户收藏记录表; 前端控制器
 * </p>
 *
 * @author tao
 * @since 2024-11-01
 */
@RestController
@RequestMapping("/uAccountCollect")
    public class UAccountCollectController {

    @Autowired
    private IUAccountCollectService uAccountCollectService;

    /**
    * 获取相关列表
     * @param current
     * @param size
     * @return
    */
    @GetMapping("/getUAccountCollectList")
    public Result<?> getUAccountCollectList(@RequestParam(value = "current", required = false, defaultValue = "1")Integer current,
                                            @RequestParam(value = "size", required = false, defaultValue = "10")Integer size,
                                            UAccountCollect uAccountCollect){

        Page<UAccountCollect> page = new Page<>(current,size);
        QueryWrapper<UAccountCollect> queryWrapper=new QueryWrapper<>(uAccountCollect);
        queryWrapper.orderByDesc("id");
        return Result.ok(uAccountCollectService.page(page,queryWrapper));
    }

    /**
         * 根据ID查询信息
         */
    @GetMapping("/getUAccountCollectById")
    public Result<?> getUAccountCollectById(Integer id){
        return Result.ok(uAccountCollectService.getById(id));
    }

    /**
         * 修改
         */
    @PutMapping("/updateUAccountCollect")
    @Transactional
    public Result<?> updateUAccountCollect(@RequestBody UAccountCollect uAccountCollect){
        return Result.ok(uAccountCollectService.updateById(uAccountCollect));
    }

    /**
         * 新增
         */
    @PostMapping("/addUAccountCollect")
    public Result<?> addUAccountCollect(@RequestBody UAccountCollect uAccountCollect){
        return Result.ok(uAccountCollectService.save(uAccountCollect));
    }

    /**
         * 删除
         */
    @DeleteMapping("/deleteUAccountCollectById")
    @Transactional
    public Result<?> deleteUAccountCollect(Integer id){
        return Result.ok(uAccountCollectService.removeById(id));
    }


    /**
     * 移动端取消收藏
     */
    @DeleteMapping("/cancelCollect")
    @Operation(summary = "移动端取消收藏", description = "移动端取消收藏")
    @Transactional
    public Result<?> cancelCollect(@RequestBody UAccountCollect accountCollect){
        QueryWrapper<UAccountCollect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_code",accountCollect.getCategoryCode());
        queryWrapper.eq("uid",accountCollect.getUid());
        return Result.ok(uAccountCollectService.remove(queryWrapper));
    }
}

